package com.autocar.reader.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.autocar.reader.R;

public class ViewUtil {

	public static Dialog getLoadDialog(Context context, String data) {
		return getLoadDialog(context, data, true);
	}

	public static Dialog getLoadDialog(Context context, String data,
			boolean cancelFlag) {
		View view = LayoutInflater.from(context).inflate(
				R.layout.loading_info_layout, null);
		TextView tv = (TextView) view
				.findViewById(R.id.loading_info_tv_content);
		tv.setText(data);
		Dialog dg = new Dialog(context);
		dg.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dg.getWindow().setBackgroundDrawableResource(R.color.transparent);
		dg.setCancelable(cancelFlag);
		dg.setContentView(view);
		return dg;
	}

	public static PopupWindow getPopupWindow(View view, boolean flag) {
		PopupWindow mPopup = new PopupWindow(view.getContext());
		mPopup.setWidth(200);
		mPopup.setHeight(150);
		// measureView(view);
		// mPopup.setWidth(view.getMeasuredWidth());
		// mPopup.setWidth(view.getMeasuredHeight());
		ColorDrawable dw = new ColorDrawable(-00000);
		mPopup.setBackgroundDrawable(dw);
		mPopup.setFocusable(flag);
		mPopup.setOutsideTouchable(flag);
		mPopup.setContentView(view);
		return mPopup;
	}

	public static PopupWindow getPopupWindow(View view, int width, int height,
			boolean flag) {
		PopupWindow mPopup = new PopupWindow(view.getContext());
		mPopup.setWidth(width);
		mPopup.setHeight(height);
		ColorDrawable dw = new ColorDrawable(-00000);
		mPopup.setBackgroundDrawable(dw);
		mPopup.setFocusable(flag);
		mPopup.setOutsideTouchable(flag);
		mPopup.setContentView(view);
		return mPopup;
	}

	public static void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}

		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
					MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0,
					MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}

}
